// 1.连接数据库
// 1.1 引入mongoose
const mongoose = require('mongoose');
// 1.2
mongoose.connect('mongodb://localhost:27017/react-demo',{useNewUrlParser:true,useUnifiedTopology: true}, function(err){
  if(err){
    console.log('Connection Error:' + err)
  }else{
    console.log('Connection success!')
  }
});

mongoose.set('useFindAndModify', false);

// 1.3 获取连接对象
const conn = mongoose.connection;
// 1.4 绑定连接完成的监听
conn.on('connected',function () {
  console.log('数据库连接成功')
});

// 2. 定义出对应特定集合的Model并向外暴露
// 2.1 字义Schema
const userSchema = mongoose.Schema({
  username:{type:String,required:true}, //用户名
  password:{type:String,required:true}, // 密码
  type:{type:String,required:true},     // 用户类型： 萌新/老板
  header:{type:String}, // 头像名称
  post:{type:String}, //职位
  info:{type:String}, // 个人或职位简介
  company:{type:String}, // 公司名称
  salary:{type:String}, // 月薪
});

// 2.2 定义Model(与集合对应，可以操作集合)
const UserModel = mongoose.model('user',userSchema);
// 2.3 向外暴露Model

// exports.Usermodel = UserModel;
exports.UserModel = UserModel;

const chatSchema = mongoose.Schema({
  from:{type:String,required:true},
  to:{type:String,required:true},
  chat_id:{type:String,required:true},
  content:{type:String,required:true},
  read:{type:Boolean,default:false},
  create_time:{type:Number}
});

const ChatModel = mongoose.model('chat',chatSchema);

exports.ChatModel = ChatModel;